RStudio has many data sets already loaded in. The example below uses preloaded data direct from RStudio example dataset: mtcars.
Read about the mtcars data set.
In the rmd file, you will see how you can load your own dataset from either 1) an online source using a URL or 2) a local file on your own computer.
#Load our data
survey = read.csv("HobbiesCOVID19.csv")
# Remove the Timestamp column as it is not necessary for our report
survey$Timestamp <- NULL
# Quick look at the structure of data
str(survey)
## 'data.frame': 29 obs. of 9 variables:
## $ age : chr "17-23" "17-23" "17-23" "17-23" ...
## $ gender : chr "Male" "Female" "Female" "Female" ...
## $ covidhobbyno : int 2 1 0 6 2 3 6 0 4 3 ...
## $ covidhobbytype: chr "Cooking/baking, Learning Japanese" "Sports/exercise, Cooking/baking" "Literature, Music, Sports/exercise, Video games, Movies/TV shows" "Literature, Music, Sports/exercise, Arts, Video games, Movies/TV shows" ...
## $ reliance : int 3 4 0 4 3 2 3 5 4 2 ...
## $ continue : chr "Yes" "No" "Yes" "Yes" ...
## $ nowhobbyno : int 2 0 4 4 1 2 3 0 3 2 ...
## $ nowhobbytype : chr "Cooking/baking, Learning Japanese" "" "Literature, Sports/exercise, Video games" "Music, Sports/exercise, Video games, Movies/TV shows" ...
## $ discontinue : chr "" "Not enough time, Was not suitable for me/became bored" "Not enough time" "Not enough time, Bored" ...
# Quick look at top 5 rows of data
head(survey)
## age gender covidhobbyno
## 1 17-23 Male 2
## 2 17-23 Female 1
## 3 17-23 Female 0
## 4 17-23 Female 6
## 5 17-23 Male 2
## 6 17-23 Female 3
## covidhobbytype
## 1 Cooking/baking, Learning Japanese
## 2 Sports/exercise, Cooking/baking
## 3 Literature, Music, Sports/exercise, Video games, Movies/TV shows
## 4 Literature, Music, Sports/exercise, Arts, Video games, Movies/TV shows
## 5 Sports/exercise, Movies/TV shows
## 6 Music, Arts, Video games, Movies/TV shows
## reliance continue nowhobbyno
## 1 3 Yes 2
## 2 4 No 0
## 3 0 Yes 4
## 4 4 Yes 4
## 5 3 Yes 1
## 6 2 Yes 2
## nowhobbytype
## 1 Cooking/baking, Learning Japanese
## 2
## 3 Literature, Sports/exercise, Video games
## 4 Music, Sports/exercise, Video games, Movies/TV shows
## 5 Movies/TV shows
## 6 Music, Movies/TV shows
## discontinue
## 1
## 2 Not enough time, Was not suitable for me/became bored
## 3 Not enough time
## 4 Not enough time, Bored
## 5 Lack of motivation
## 6 Not enough time
# Our data has 29 rows and 9 columns
# Size of the data
dim(survey)
## [1] 29 9
# R's classification of survey's data
class(survey)
## [1] "data.frame"
## R's classification of variables
str(mtcars)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
#sapply(mtcars, class)
Summary:
How did COVID-19 affected people’s hobbies → Number of hobbies during and after covid, bar graph
# Number of hobbies picked up during quarantine
barplot(table(survey$covidhobbyno), main="Number of hobbies picked up during quarantine")
#Number of hobbies now
barplot(table(survey$nowhobbyno), main="Number of hobbies now")
Summary:
What kind of hobbies people start doing during Covid?
# Install packages ggplot2
library(ggplot2)
p = ggplot(survey, aes(x=covidhobbytype))
p + geom_bar()
Is there a linear relationship between the number of hobbies and people’s reliance of hobbies (number of hobbies vs comfort level)
# Construct a scatter plot
plot(survey$covidhobbyno, survey$reliance)
# Calculate the linear regression model to draw on the scatter plot
L = lm(survey$covidhobbyno ~ survey$reliance)
summary(L)
##
## Call:
## lm(formula = survey$covidhobbyno ~ survey$reliance)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.9785 -0.7450 -0.2779 0.7221 3.4885
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.8109 0.7267 2.492 0.0191 *
## survey$reliance 0.2335 0.2122 1.100 0.2809
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.473 on 27 degrees of freedom
## Multiple R-squared: 0.04291, Adjusted R-squared: 0.007466
## F-statistic: 1.211 on 1 and 27 DF, p-value: 0.2809
L$coeff
## (Intercept) survey$reliance
## 1.8108883 0.2335244
abline(L)
# Caluculate the linear correlation coefficient
cor(survey$covidhobbyno, survey$reliance)
## [1] 0.2071563
Summary:
Style: APA
This quick reference guide will cover some basic RMarkdown for use in your projects.
Here is a basic list:
To do 1
To do 2
To do 3
Here is a simple table.
| Tables | Are | Cool |
|---|---|---|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
Here is am image. It has not been adjusted in the rmd file, so represents the true size of the original image. This image is sourced directly from an online url.
To learn more about adding images directly from your own computer, see the comments in this rmd file.
Image source: https://petcube.com/blog/10-all-important-kitten-supplies-infographic/
Below you will find a video embedded into your RMarkdown file. Change the YouTube link in the rmd file to get a different video.
You can even use LaTeX in an RMarkdown document!
For example, how could you work out \(\sum_{i=1}^{5} x_{i}^3\)?
Here is an R code chunk:
Try the following commands in R.
1+ exp(3) + sin(0.5)
x=c(1,2,3)
x^2
sum(x)
Here is some in-line code in-line code. You can put any R code here for display, e.g. sum(x)
Check out the resources below for more information on RMarkdown.